package com.kb5173.base.demo.dubbo.api;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.kb5173.base.demo.protocal.DemoServiceApi;

public class DubboApiConsumer {

	public static void main(String[] args) throws Exception {
		
//		dubboProtocol();
		
		hessianProtocol();
		
		//dubbo调用原生Hessian服务
//		dubboToHessian();
		
		System.in.read();
	}
	
	private static ApplicationConfig ac = new ApplicationConfig("apiInvoker");
	
	public static void dubboProtocol(){

		RegistryConfig rc = new RegistryConfig();
		rc.setAddress("www.wzitech.com");
		rc.setProtocol("zookeeper");
		rc.setPort(2181);
		
		MonitorConfig mc = new MonitorConfig();
		mc.setAddress("www.wzitech.com:7070");
		mc.setProtocol("dubbo");
		
		ReferenceConfig<DemoServiceApi> rfc = new ReferenceConfig<DemoServiceApi>();
		rfc.setRegistry(rc);
		rfc.setInterface(DemoServiceApi.class);
		rfc.setApplication(ac);
		rfc.setProtocol("dubbo");
		rfc.setOwner("springAnnotationExportor");
		rfc.setMonitor(mc);
		rfc.setGroup("annotation");
		
		DemoServiceApi da = rfc.get();
		long start = System.currentTimeMillis();
		for (int i = 0; i < 1000; i++) {
			System.out.println("========="+i+"=========" + da.demoServiceApi());
		}
		System.out.println((System.currentTimeMillis()-start)/1000 +"S");
	}
	
	public static void hessianProtocol(){
		RegistryConfig rc = new RegistryConfig();
		rc.setAddress("www.wzitech.com");
		rc.setProtocol("zookeeper");
		rc.setPort(2181);
		
		MonitorConfig mc = new MonitorConfig();
		mc.setAddress("www.wzitech.com:7070");
		mc.setProtocol("dubbo");
		
		ReferenceConfig<DemoServiceApi> rfc = new ReferenceConfig<DemoServiceApi>();
		rfc.setRegistry(rc);
		rfc.setInterface(DemoServiceApi.class);
		rfc.setApplication(ac);
		rfc.setProtocol("hessian");
		rfc.setMonitor(mc);
		rfc.setGroup("annotation");
		
		DemoServiceApi da = rfc.get();
		long start = System.currentTimeMillis();
		for (int i = 0; i < 1000; i++) {
			System.out.println("========="+i+"=========" + da.demoServiceApi());
		}
		System.out.println((System.currentTimeMillis()-start)/1000 +"S");
	}
	
	public static void dubboToHessian(){
		String url = "hessian://localhost:9091/"+DemoServiceApi.class.getName();
		ReferenceConfig<DemoServiceApi> rfc = new ReferenceConfig<DemoServiceApi>();
		rfc.setUrl(url);
		rfc.setInterface(DemoServiceApi.class);
		rfc.setApplication(ac);
//		rfc.setProtocol("hessian");
		
		DemoServiceApi da = rfc.get();
		long start = System.currentTimeMillis();
		for (int i = 0; i < 1000; i++) {
			System.out.println("========="+i+"=========" + da.demoServiceApi());
		}
		System.out.println((System.currentTimeMillis()-start)/1000 +"S");
		
	}
}
